package de.jonasblatt.rpiclient;

import java.util.Scanner;

import de.jonasblatt.networkapi.rpi.network.commands.client.CCBlinkGpio;
import de.jonasblatt.networkapi.rpi.network.commands.client.CCRequestGpioValue;
import de.jonasblatt.networkapi.rpi.network.commands.client.CCRequestSystemVal;
import de.jonasblatt.networkapi.rpi.network.commands.client.CCSayHello;
import de.jonasblatt.networkapi.rpi.network.commands.client.CCSetGpioOff;
import de.jonasblatt.networkapi.rpi.network.commands.client.CCSetGpioOn;
import de.jonasblatt.networkapi.rpi.networkapi.rpi.client.Client;
import de.jonasblatt.networkapi.rpi.networkapi.rpi.client.ClientListener;

public class SmallClient implements ClientListener {
	private Client client;

	public void connect() {
		System.out.println("Insert IP or Raspberry Server name:");

		Scanner input = new Scanner(System.in);
		String in = "";

		in = input.nextLine();

		this.client = new Client(in);
		this.client.addListener(this);
		this.client.connect();
		System.out.println("Conncecting to Server..");
		do {
		} while (this.client.isConnecting());
		if (this.client.isAccepted()) {
			System.out.println("actions: ");
			System.out.println("     exit            - closing this session, sayhello, temp");
			System.out.println("     sayhello        - say hello to pi");
			System.out.println("     temp            - get CPU Temp");
			System.out.println();
			System.out.println("  gpio out:");
			System.out.println("     gpioon %gpioxx  - set gpioxx on");
			System.out.println("     gpiooff %gpioxx - set gpioxx off");
			System.out.println("     blink %gpioxx   - gpioxx is blinking");
			System.out.println();
			System.out.println("  gpio in:");
			System.out.println("     gpioval %gpioxx - get value of gpioxx");
			do {
				System.out.print("> ");
				in = input.nextLine();
				if (!this.action(in)) {
					System.out.println("no valid action!");
				}
			} while (!in.equals("exit"));
		}
		input.close();
	};

	private boolean action(String in) {
		String[] inparm = in.split(" ");
		if (inparm.length == 0) {
			return false;
		}
		switch (inparm[0]) {
		case "sayhello":
			this.client.sendCommand(new CCSayHello(this.client.getClientID(), "Papa"));
			break;
		case "gpioon":
			if (inparm.length == 2) {
				this.client.sendCommand(new CCSetGpioOn(this.client.getClientID(), inparm[1]));
			}
			break;
		case "gpiooff":
			if (inparm.length == 2) {
				this.client.sendCommand(new CCSetGpioOff(this.client.getClientID(), inparm[1]));
			}
			break;
		case "blink":
			if (inparm.length == 2) {
				this.client.sendCommand(new CCBlinkGpio(this.client.getClientID(), inparm[1], 10, 1000));
			}
			break;
		case "gpioval":
			if (inparm.length == 2) {
				this.client.sendCommand(new CCRequestGpioValue(this.client.getClientID(), inparm[1]));
			}
			break;
		case "temp":
			this.client.sendCommand(new CCRequestSystemVal(this.client.getClientID(), "temp"));
			break;
		case "exit":
			this.client.closeConnection();
			break;
		default:
			return false;
		}
		return true;
	}

	@Override
	public void onClientIdConfirmed(Client client, int clientId) {
		// TODO Auto-generated method stub
		System.out.println("onClientIdConfirmed");
	}

	@Override
	public void onClientNotConnectedToHost(Client client) {
		// TODO Auto-generated method stub
		System.out.println("onClientNotConnectedToHost");
	}

	@Override
	public void onServerJoinDenied(Client client) {
		// TODO Auto-generated method stub
		System.out.println("onServerJoinDenied");
	}

	@Override
	public void onGpioValueArrived(Client client, String gpio, boolean value) {
		System.out.println(gpio + ": " + value);
	}

}
